home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’89 / JPrint XCMD / ReplaceStr.p < prev    next >
Encoding:
Text File  |  1989-03-18  |  1.9 KB  |  86 lines  |  [TEXT/EDIT]

  1. {$R-}
  2.  
  3. (*
  4.     ReplaceStr.p -- An XFNC which uses the Munger (the all-purpose trap!)
  5.  
  6.     Usage:
  7.         put ReplaceStr <Source Containter> <Target Str> <Replacement Str>
  8.             into <Destination Container>
  9.  
  10.     Return Value:
  11.         The <Source Container>, with all instances of <Target Str>
  12.         found in the <Source Container> replaced by <Replacement Str>
  13.         No recursive replacement is tried, nor is the <Source Container>
  14.         effected.
  15.  
  16.     Copyright © 1989 by D. Jay Newman.  All rights reserved.
  17. *)
  18.  
  19. {$S ReplaceStr }     {Segment name must be the same as the command name.}
  20.  
  21. UNIT DummyUnit;
  22.  
  23. INTERFACE
  24.  
  25. USES MemTypes, QuickDraw, OSIntf, ToolIntf, HyperXCmd;
  26.  
  27. PROCEDURE EntryPoint (paramPtr: XCmdPtr);
  28.  
  29. IMPLEMENTATION
  30.  
  31.     TYPE Str31 = String[31];
  32.  
  33.     PROCEDURE ReplaceStr (paramPtr: XCmdPtr); FORWARD;    {So it can be called}
  34.  
  35.     PROCEDURE EntryPoint (paramPtr: XCmdPtr);
  36.     BEGIN
  37.         ReplaceStr (paramPtr);
  38.     END;
  39.  
  40.     PROCEDURE ReplaceStr (paramPtr: XCmdPtr);
  41.     VAR
  42.         h:            Handle;        {Handle to Source text}
  43.         offset:        LONGINT;
  44.         p:            Ptr;        {Pointer to calculate length}
  45.         len1:        LONGINT;    {Length of target string}
  46.         len2:        LONGINT;    {Length of replacement string}
  47.         anErr:        OSErr;        {Result of HandToHand}
  48.  
  49.     {$I XCmdGlue.inc }
  50.  
  51.     BEGIN
  52.         WITH paramPtr^ DO
  53.             BEGIN
  54.                 IF paramCount <> 3 THEN EXIT (ReplaceStr);    {Need all three params}
  55.  
  56.                 h := params [1];
  57.  
  58.                 HLock (params[2]);                            {Lock to avoid mem moves}
  59.                 p := params[2]^;
  60.                 ScanToZero (p);
  61.                 len1 := Ord4 (p) - Ord4 (params[2]^);
  62.  
  63.                 HLock (params[3]);                            {Lock to avoid mem moves}
  64.                 p := params[3]^;
  65.                 ScanToZero (p);
  66.                 len2 := Ord4 (p) - Ord4 (params[3]^);
  67.  
  68.                 offset := 0;
  69.                 WHILE offset >= 0 DO
  70.                     BEGIN
  71.                         offset := Munger (h, offset, params[2]^, len1,
  72.                                 params[3]^, len2);
  73.                     END;
  74.  
  75.                 HUnlock (params[2]);                        {Unlock like good boys}
  76.                 HUnlock (params[3]);
  77.  
  78.                 anErr := HandToHand (h);
  79.                 IF anErr <> noErr THEN
  80.                     SysBeep (1);
  81.  
  82.                 returnValue := h;
  83.             END;
  84.     END;
  85.  
  86. END.